//
//  ViewController.swift
//  图片轮播器
//
//  Created by ya_ch.ao@qq.com on 2016/12/9.
//  Copyright © 2016年 com.zone. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var topLabel: UILabel!
    @IBOutlet weak var descLabel: UILabel!
    @IBOutlet weak var letfBtn: UIButton!
    @IBOutlet weak var rightBtn: UIButton!
    @IBOutlet weak var imageView: UIImageView!
    
    // 设置当前图片的位置
    var index = 0
    // 获取plist文件的路径
    var path: String = Bundle.main.path(forResource: "imageDate", ofType: "plist")!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//  MARK: Action
    
    @IBAction func leftBtnOnClick(_ sender: UIButton) {
        self.index -= 1
        btnClickChange()
        
    }
    @IBAction func rightBtnOnClick(_ sender: UIButton) {
        self.index += 1
        btnClickChange()
    }

    func btnClickChange() {
        // 解析plist文件
        let imageDicts = NSArray(contentsOfFile: path)
        
        // 设置头部label的内容
        self.topLabel.text = "\(index+1)/\(imageDicts!.count)"
        
        // 设置尾部label的内容
        let desc: [String] = imageDicts!.value(forKey: "description") as! [String]
        self.descLabel.text = desc[index]
        
        // 设置图片
        let imageName: NSArray = imageDicts!.value(forKey: "name") as! NSArray
        self.imageView.image = UIImage(named: imageName[index] as! String)
        
        // 改变按钮状态
        self.letfBtn.isEnabled = (self.index != 0)
        self.rightBtn.isEnabled = (self.index != 4)
    }
    
}

